package at.fhj.itm11.forum.service.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;

import at.fhj.itm11.forum.data.CommentDAO;
import at.fhj.itm11.forum.data.PostDAO;
import at.fhj.itm11.forum.data.UserDAO;
import at.fhj.itm11.forum.model.Comment;
import at.fhj.itm11.forum.service.CommentService;
import at.fhj.itm11.forum.service.converter.CommentDTOConverter;
import at.fhj.itm11.forum.service.converter.DTOConverter;
import at.fhj.itm11.forum.service.dtos.CommentDTO;

@Stateless
@Named
public class CommentServiceImpl implements CommentService {

	@Inject
	@Named("commentDAOImpl")
	private CommentDAO commentDao;

	@Inject
	@Named("userDAOImpl")
	private UserDAO userDao;

	@Inject
	@Named("postDAOImpl")
	private PostDAO postDao;

	private DTOConverter<CommentDTO, Comment> commentDtoConverter = new CommentDTOConverter();

	@Override
	public void register(CommentDTO commentDto) {
		Comment comment = new Comment();

		comment.setDate(commentDto.getDate());
		comment.setText(commentDto.getText());
		comment.setUser(userDao.findById(commentDto.getUser().getId()));
		comment.setPost(postDao.findById(commentDto.getPost().getId()));

		commentDao.register(comment);
	}

	@Override
	public List<CommentDTO> findCommentsByPostOrderedByDate(Long postId) {
		return commentDtoConverter.convertToDto(commentDao
				.findCommentsByPostOrderedByDate(postId));
	}

	@Override
	public CommentDTO findCommentById(Long id) {
		return commentDtoConverter.convertToDto(commentDao.findCommentById(id));
	}

	public void setCommentDao(CommentDAO commentDao) {
		this.commentDao = commentDao;
	}

	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}

	public void setPostDao(PostDAO postDao) {
		this.postDao = postDao;
	}

}
